<script setup lang="ts">
import { ref } from 'vue';
import { AIReplyMessageAPI } from '@/services/message';
import { onShow } from '@dcloudio/uni-app'
const messages = ref([]);
const isLoading = ref(false);
const fetchAIReply = async () => {
  const pages = getCurrentPages();
  const currentPage = pages[pages.length - 1]; 
  const query = currentPage.options; 

  const recordId = query.recordId; 
  try {
    isLoading.value = true;
    const response = await AIReplyMessageAPI(recordId);
    const aiReply = response.data.data;
    messages.value.push({ text: aiReply, type: 'ai' });
  } catch (error) {
    console.error('获取AI回复失败:', error);
  } finally {
    isLoading.value = false;
  }
};
onShow(() => {
  fetchAIReply();
});

</script>

<template>
  <view class="chat-container"  @scroll="handleScroll">
     <view v-if="isLoading" class="loading-container">
          <view class="loading-spinner"></view>
        </view>
    <!-- 渲染用户回复的消息 -->
      <view v-for="(message, index) in messages" :key="index" class="message-card" :class="{'user-reply': message.type === 'user', 'ai-reply': message.type === 'ai'}">
          <view class="avatar-container"></view>
          <view class="message-content">
            {{ message.text }}
          </view>
        </view>
  </view>
</template>

<style scoped>
  /* 样式调整 */
  .chat-container {
    display: flex;
    flex-direction: column;
    height: 100vh;
    padding: 20px;
  }

  .message-card {
    display: flex;
    align-items: flex-start;
    margin-bottom: 10px;
  }

  .avatar-container {
    margin-right: 10px;
  }

  .message-content {
    max-width: 70%;
    background-color: #e0f2f1;
    padding: 10px;
    border-radius: 10px;
  }

  .loading-container {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 50px;
  }

  .loading-spinner {
    width: 20px;
    height: 20px;
    border-radius: 50%;
    border: 2px solid #00bcd4;
    border-top-color: transparent;
    animation: spin 1s linear infinite;
  }

  @keyframes spin {
    0% {
      transform: rotate(0deg);
    }
    100% {
      transform: rotate(360deg);
    }
  }
</style>
